*****************************************************************************
*------------------------**------------------------------**------------------
*       Program Purpose: Prepare Dataset for Kenya Resilience Study 
*							for Technical Consortium						*
*							

*                              CHILD REGRESSIONS                            *
* 					 Sara Signorelli , November 9th 2015
*------------------------**------------------------------**------------------
******************************************************************************


****************** Set the directories 
global out  "D:\Users\SSIGNORELLI\Dropbox (IFPRI)\AR research\Kenya_Resilience\out\Anthro"



***********************************************
********** CHILD LEVEL ANALYSIS ***************
***********************************************

set more off
use   "$out\KEN_DHS_temp.dta", clear

***** Controls
replace w_age=w_age-(c_age/12)
gen rural=(urbrur==2)
gen female_child=(c_sex==2)
gen female_head=(sex_head==2)
tab regcode  if dry_land>0, gen(regdry)
tab regcode, gen(regall)
tab year, gen(yrd)
gen temp=indweight*100
gen i_weight=round(temp)
drop temp



** CONSTRUCTION OF PSEUDO PANEL USING PSMATCHING
global pp_controls c_age female_child w_age w_edu rural female_head ih_hv207_radio 
global pp_outcomes ic_stunted_moderate ic_underweight_moderate ic_wasted_moderate  

gen all_land=1

cd "$out"
foreach file in probit_ppanel_dry probit_ppanel_all {
cap rm `file'.xml
cap rm `file'.txt
}

foreach cond in dry all {
foreach yvar in $pp_outcomes {
forval y=1/4 {
probit `yvar' $pp_controls reg`cond'* if yrd`y'==1 & `cond'_land>0 [pw=indweight], cluster(cluster)
outreg2 using probit_ppanel_`cond',  ctitle("`var'") excel label
predict `yvar'_`cond'_`y'
xtile `yvar'_`cond'_`y'_q=`yvar'_`cond'_`y', n(5)
drop `yvar'_`cond'_`y'
}
}
}

foreach cond in dry all {
gen pp_grp_stunted_`cond'=. 
gen pp_grp_underwgt_`cond'=.  
gen pp_grp_wasted_`cond'=. 


forval y=1/4 {
replace pp_grp_stunted_`cond'= ic_stunted_moderate_`cond'_`y'_q        if yrd`y'==1 & `cond'_land>0 
replace pp_grp_underwgt_`cond'=ic_underweight_moderate_`cond'_`y'_q    if yrd`y'==1 & `cond'_land>0
replace pp_grp_wasted_`cond'= ic_wasted_moderate_`cond'_`y'_q          if yrd`y'==1 & `cond'_land>0 
drop ic_stunted_moderate_`cond'_`y'_q  ic_underweight_moderate_`cond'_`y'_q ic_wasted_moderate_`cond'_`y'_q 
}
}



** BIO VARIABLES
*******************************************
foreach var in temperature Ltemperature temp_season rainfall Lrainfall  PDSI NDVI {
gen `var'=.
}

forval i=1993(5)2008 {
local j=`i'-1
local j2=`i'-2
replace     temperature=bio1_`j'   if year==`i'
replace     Ltemperature=bio1_`j2' if year==`i'
replace     temp_season=bio4_`j'   if year==`i'
replace     rainfall=bio12_`j'     if year==`i'
replace     Lrainfall=bio12_`j2'   if year==`i'
replace     PDSI=pdsi`j'           if year==`i'
replace     NDVI=NDVI_`j'          if year==`i'
}

gen drought=-PDSI
replace NDVI=NDVI/10000

lab var ic_underweight_low   "Child is slightly underweight (waz<-1)" 
lab var ic_wasted_low        "Child is slightly wasted (whz<-1)" 
lab var ic_stunted_low       "Child is slightly stunted (haz<-1)" 
lab var ic_underweight_moderate "Child is moderately underweight (waz<-2)" 
lab var ic_wasted_moderate      "Child is moderately wasted (whz<-2)" 
lab var ic_stunted_moderate   	"Child is moderately stunted (haz<-2)" 
lab var ic_underweight_severe   "Child is severely underweight (waz<-3)" 
lab var ic_wasted_severe        "Child is severely wasted (whz<-3)" 
lab var ic_stunted_severe	    "Child is severely stunted (haz<-3)" 
lab var ic_haz06 				"height for age score"
lab var ic_waz06 				"weight for age score"
lab var ic_whz06				"weight for height score"
lab var yrd1					"year 1993"
lab var yrd2					"year 1998"
lab var yrd3					"year 2003"
lab var yrd4 					"year 2008" 
lab var regdry1					"Coast Region"
lab var regdry2					"Eastern Region"
lab var regdry3					"Rift Valley Region"
lab var regdry4					"North Eastern Region"
lab var rainfall  				"Mean annual rainfall"
lab var Lrainfall  				"Lagged mean annual rainfall"
lab var temperature  			"Mean annual temperature"
lab var Ltemperature  			"Lagged mean annual temperature"
lab var drought					"Inverse PDSI"
lab var NDVI					"NDVI"
lab var soc_total				"Soil organic carbon total (permiles)"
lab var  pp_grp_stunted_dry         "Pseudo-panel group for dry areas: stunting"
lab var pp_grp_underwgt_dry         "Pseudo-panel group for dry areas: underweight" 
lab var pp_grp_wasted_dry           "Pseudo-panel group for dry areas: wasting"
lab var  pp_grp_stunted_all         "Pseudo-panel group for all areas: stunting"
lab var pp_grp_underwgt_all         "Pseudo-panel group for all areas: underweight" 
lab var pp_grp_wasted_all           "Pseudo-panel group for all areas: wasting"

rename ic_underweight_low underwght_l
rename ic_wasted_low  wasted_l
rename ic_stunted_low stunted_l
rename ic_underweight_moderate  underwght_m
rename ic_wasted_moderate wasted_m 
rename ic_stunted_moderate stunted_m
rename ic_underweight_severe underwght_s
rename ic_wasted_severe wasted_s
rename ic_stunted_severe stunted_s

** Summary statistics
preserve
collapse stunted_m underwght_m wasted_m rainfall temperature drought [pw=indweight], by (regname year)
order year, after(regname)
export excel using "$out\SumStat.xls", firstrow(varlabels) replace
restore


gen arid_area=(dry_land>0)

** Construct pseudo-panel groups
foreach var in dry all {
tab pp_grp_stunted_`var', gen(pstunt_`var')
tab pp_grp_underwgt_`var', gen(punwgt_`var')
tab pp_grp_wasted_`var', gen(pwaste_`var')
}


****** regression variables *****************************
************************************************

* Stunting
global anthro1  stunted_m stunted_s 
global zscore1  ic_haz06
global panel1_dry pp_grp_stunted_dry
global panel1_all pp_grp_stunted_all
global control1_dry pstunt_dry2-pstunt_dry5
global control1_all arid_area pstunt_all2-pstunt_all5 

* Underweight
global anthro2  underwght_m underwght_s
global zscore2  ic_waz06
global panel2_dry pp_grp_underwgt_dry
global panel2_all pp_grp_underwgt_all
global control2_dry punwgt_dry2-punwgt_dry5 
global control2_all arid_area punwgt_all2-punwgt_all5 

* Wasting
global anthro3  wasted_m wasted_s
global zscore3  ic_whz06
global panel3_dry pp_grp_wasted_dry
global panel3_all pp_grp_wasted_all
global control3_dry pwaste_dry2-pwaste_dry5    
global control3_all arid_area pwaste_all2-pwaste_all5

** bio variables
global bio1 temperature drought NDVI
global bio2 temperature temp_season rainfall rain_season

** Resilience interactions
gen nonagri_job=(w_occupation==1 | w_occupation==2 | w_occupation==3 | w_occupation==7 | w_occupation==8 | w_occupation==9  | ///
				occupation_partner==1 | occupation_partner==2 | occupation_partner==3 | occupation_partner==7 | occupation_partner==8 | occupation_partner==9 )
replace nonagri_job=. if occupation_partner==. & w_occupation==.

gen tempXnagri=temperature*nonagri_job
gen DroughtXnagri=drought*nonagri_job
gen RainXnagri=rainfall*nonagri_job

gen tempXedu=temperature*w_edu
gen DroughtXedu=drought*w_edu
gen RainXedu=rainfall*w_edu

global resilience1 tempXnagri  DroughtXnagri  nonagri_job
global resilience2 tempXedu DroughtXedu w_edu 

***********************************************************
***********************************************************



** Simulations **
gen drought_sim=drought+0.5*drought
gen temperature_sim=temperature+2
gen ndvi_sim=NDVI-0.5*NDVI


*************************************************************
********** REGRESSION ANALYSIS: ANTHROPOMETRICS *************
*************************************************************


cd "$out"
set more off

foreach file in probit_res_dry probit_res_all reg_res_dry reg_res_all   {
cap rm `file'.xml
cap rm `file'.txt
}

*** PROBIT ON LIKELIHOOD OF BEING STUNTED, UNDERWEIGHT OR WASTED ***
********************************************************************
foreach cond in dry all {
forval i=1/3 {
foreach var in ${anthro`i'} {


** 1) Without Resilience controls
probit `var' ${control`i'_`cond'} $bio1 yrd2-yrd4  if `cond'_land>0 [pw=indweight],  cluster(cluster)
outreg2 using probit_res_`cond',  ctitle("`var'") excel label 

predict `var'_hat_`cond'


** Simulate temperature shock
gen temperature_temp=temperature
replace temperature=temperature_sim
predict `var'_hat_simtemp_`cond'
replace temperature=temperature_temp
drop temperature_temp

** Simulate drought shock
gen drought_temp=drought
replace drought=drought_sim
predict `var'_hat_simdr_`cond'
replace drought=drought_temp
drop drought_temp

** Simulate NDVI shock
gen NDVI_temp=NDVI
replace NDVI=ndvi_sim
predict `var'_hat_simndvi_`cond'
replace NDVI=NDVI_temp
drop NDVI_temp

** Simulate drought and temperature shock
gen drought_temp=drought
gen temperature_temp=temperature
replace drought=drought_sim
replace temperature=temperature_sim
predict `var'_hat_simtemdr_`cond'
replace drought=drought_temp
replace temperature=temperature_temp
drop drought_temp temperature_temp

** Simulate drought and temperature shock
gen drought_temp=drought
gen temperature_temp=temperature
gen NDVI_temp=NDVI
replace drought=drought_sim
replace temperature=temperature_sim
replace NDVI=ndvi_sim
predict `var'_hat_simall_`cond'
replace drought=drought_temp
replace temperature=temperature_temp
replace NDVI=NDVI_temp
drop drought_temp temperature_temp NDVI_temp
}
}
}


foreach cond in dry all {
forval i=1/3 {
foreach var in ${anthro`i'} {


** 1) With Resilience controls
probit `var' ${control`i'_`cond'} $resilience1 $bio1 yrd2-yrd4 [pw=indweight] if `cond'_land>0,  cluster(cluster)
outreg2 using probit_res_`cond',  ctitle("`var'", nonagri job) excel label  
probit `var' ${control`i'_`cond'} $resilience2 $bio1 yrd2-yrd4 [pw=indweight] if `cond'_land>0,  cluster(cluster)
outreg2 using probit_res_`cond',  ctitle("`var'", edu) excel label

} 
}
}


*** REGRESSIONS ON ZSCORES ** 
*****************************

foreach cond in dry all {
forval i=1/3 {
** 1) Without resilience controls
reg ${zscore`i'} ${control`i'_`cond'} $bio1 yrd2-yrd4 [pw=indweight] if `cond'_land>0,  cluster(cluster)
outreg2 using reg_res_`cond',  ctitle("`var'") excel label 

forval p=25(25)75 {
qreg ${zscore`i'} ${control`i'_`cond'} $bio1 yrd2-yrd4 [pw=indweight]  if `cond'_land>0, vce(robust) quantile(0.`p')
outreg2 using reg_res_`cond',    ctitle("`var'", p`p') excel label 
}
}
}


foreach cond in dry all {
forval i=1/3 {
** 2a) With resilience controls: non agri job
reg ${zscore`i'} ${control`i'_`cond'} $resilience1 $bio1 yrd2-yrd4 [pw=indweight] if `cond'_land>0,  cluster(cluster)
outreg2 using reg_res_`cond',  ctitle("`var'", non agri job) excel label 

forval p=25(25)75 {
qreg ${zscore`i'} ${control`i'_`cond'} $resilience1 $bio1 yrd2-yrd4 [pw=indweight]  if `cond'_land>0, vce(robust) quantile(0.`p')
outreg2 using reg_res_`cond',    ctitle("`var'", non agri job p`p') excel label 
}

** 2b) With resilience controls: cattle ownership
reg ${zscore`i'} ${control`i'_`cond'} $resilience2 $bio1 yrd2-yrd4 [pw=indweight] if `cond'_land>0,  cluster(cluster)
outreg2 using reg_res_`cond',  ctitle("`var'", edu) excel label  

forval p=25(25)75 {
qreg ${zscore`i'} ${control`i'_`cond'} $resilience2 $bio1 yrd2-yrd4 [pw=indweight]  if `cond'_land>0, vce(robust) quantile(0.`p')
outreg2 using reg_res_`cond',    ctitle("`var'", edu p`p') excel label 
}

}
}


** Simulations Results **
*************************

foreach cond in all dry {
foreach var in stunted underwght wasted {
tabstat `var'_m `var'_m_hat_`cond' `var'_m_hat_simtemp_`cond' `var'_m_hat_simdr_`cond' ///
   `var'_m_hat_simtemdr_`cond' if `cond'_land>0, by(year)
}
}

